/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateAccountDlg.java
 *
 * Created on May 5, 2010, 8:32:43 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.ManagementBiz;
import com.aptech.campaign.entities.Account;
import java.util.Calendar;
import javax.swing.JOptionPane;

/**
 *
 * @author Tai Nguyen
 */
public class CreateAccountDlg extends javax.swing.JDialog {

    ManagementBiz buz = new ManagementBiz();
    public static AdministratorFrm admin;

    public CreateAccountDlg(AdministratorFrm admin, boolean modal) {
        super(admin, modal);
        CreateAccountDlg.admin = admin;
        initComponents();
        setLocationRelativeTo(null);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     *
     * @return the boolean value
     */
    public boolean valid() {
        if (!txfFullName.getText().equalsIgnoreCase("") && !txfPhone.getText().equalsIgnoreCase("") && !txfUserName.getText().equalsIgnoreCase("")
                && dtcBirthday.getDate() != null && (new String(pwfPass.getPassword()).equalsIgnoreCase("") == false) && (new String(pwfConfirmPass.getPassword()).equalsIgnoreCase("") == false)) {
            return true;
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    /**
     * 
     */
    public void resetAll() {
        txfFullName.setText("");
        txfPhone.setText("");
        txfUserName.setText("");
        pwfConfirmPass.setText("");
        pwfPass.setText("");
        dtcBirthday.setDate(null);
        rdbMale.setSelected(true);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGruopGender = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        txfUserName = new javax.swing.JTextField();
        lblPassword = new javax.swing.JLabel();
        pwfPass = new javax.swing.JPasswordField();
        lblConfirmPass = new javax.swing.JLabel();
        pwfConfirmPass = new javax.swing.JPasswordField();
        lblFullName = new javax.swing.JLabel();
        txfFullName = new javax.swing.JTextField();
        lblBirthDay = new javax.swing.JLabel();
        lblPhone = new javax.swing.JLabel();
        txfPhone = new javax.swing.JTextField();
        lblGender = new javax.swing.JLabel();
        rdbMale = new javax.swing.JRadioButton();
        rdbFemale = new javax.swing.JRadioButton();
        btnCreate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        dtcBirthday = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Create Account");
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setBackground(new java.awt.Color(51, 102, 255));
        jPanel1.setForeground(new java.awt.Color(51, 102, 255));
        jPanel1.setMinimumSize(new java.awt.Dimension(394, 70));
        jPanel1.setPreferredSize(new java.awt.Dimension(394, 70));
        jPanel1.setLayout(new java.awt.BorderLayout());

        lblTitle.setBackground(new java.awt.Color(51, 102, 255));
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 20));
        lblTitle.setForeground(new java.awt.Color(51, 102, 255));
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/CREATEACCBANNER.jpg"))); // NOI18N
        jPanel1.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 0);
        getContentPane().add(jPanel1, gridBagConstraints);

        lblUserName.setText("UserName : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 15, 0);
        getContentPane().add(lblUserName, gridBagConstraints);

        txfUserName.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 171;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 0, 0);
        getContentPane().add(txfUserName, gridBagConstraints);

        lblPassword.setText("Password : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 15, 0);
        getContentPane().add(lblPassword, gridBagConstraints);

        pwfPass.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 171;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 15, 0);
        getContentPane().add(pwfPass, gridBagConstraints);

        lblConfirmPass.setText("Confirm Password : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 15, 0);
        getContentPane().add(lblConfirmPass, gridBagConstraints);

        pwfConfirmPass.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 171;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 15, 0);
        getContentPane().add(pwfConfirmPass, gridBagConstraints);

        lblFullName.setText("Full Name : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 15, 0);
        getContentPane().add(lblFullName, gridBagConstraints);

        txfFullName.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 171;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 15, 0);
        getContentPane().add(txfFullName, gridBagConstraints);

        lblBirthDay.setText("BirthDay : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 15, 0);
        getContentPane().add(lblBirthDay, gridBagConstraints);

        lblPhone.setText("Phone : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 15, 0);
        getContentPane().add(lblPhone, gridBagConstraints);

        txfPhone.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 171;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 15, 0);
        getContentPane().add(txfPhone, gridBagConstraints);

        lblGender.setText("Gender : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 15, 0);
        getContentPane().add(lblGender, gridBagConstraints);

        btnGruopGender.add(rdbMale);
        rdbMale.setSelected(true);
        rdbMale.setText("Male");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 15, 12);
        getContentPane().add(rdbMale, gridBagConstraints);

        btnGruopGender.add(rdbFemale);
        rdbFemale.setText("Female");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 0);
        getContentPane().add(rdbFemale, gridBagConstraints);

        btnCreate.setBackground(new java.awt.Color(51, 102, 255));
        btnCreate.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnCreate.setForeground(new java.awt.Color(255, 255, 255));
        btnCreate.setText("Create");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 10, 12);
        getContentPane().add(btnCreate, gridBagConstraints);

        btnCancel.setBackground(new java.awt.Color(51, 102, 255));
        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnCancel.setForeground(new java.awt.Color(255, 255, 255));
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 21, 10, 0);
        getContentPane().add(btnCancel, gridBagConstraints);

        dtcBirthday.setPreferredSize(new java.awt.Dimension(110, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 83;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 15, 0);
        getContentPane().add(dtcBirthday, gridBagConstraints);

        jLabel1.setText("(6-13 number)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(jLabel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        dispose();

    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
        if (!valid()) {
            JOptionPane.showMessageDialog(this, "Please input data in fields!");
        } else if (new String(pwfPass.getPassword()).compareTo(new String(pwfConfirmPass.getPassword())) != 0) {
            JOptionPane.showMessageDialog(this, "Confirm Password incorrect!");
        } else {
            Account acc = new Account();
            acc.setUserName(txfUserName.getText());
            acc.setPassword(String.valueOf(pwfPass.getPassword()));
            acc.setFullName(txfFullName.getText());
            Calendar ca = Calendar.getInstance();
            ca.setTime(dtcBirthday.getDate());
            java.sql.Date date = new java.sql.Date(ca.getTimeInMillis());
            acc.setBirthday(date);
            acc.setFone(txfPhone.getText());
            acc.setFone(txfPhone.getText());
            String gender = "Male";
            if (rdbFemale.isSelected()) {
                gender = "Female";
            }
            acc.setGender(gender);
            if (acc.validate()) {
                if (buz.createAccount(acc)) {
                    JOptionPane.showMessageDialog(this, buz.getLasterror());
                    admin.loadTable();
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLasterror());
                }
            } else {
                JOptionPane.showMessageDialog(this, acc.getLastError());
            }

        }
    }//GEN-LAST:event_btnCreateActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                CreateAccountDlg dialog = new CreateAccountDlg(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreate;
    private javax.swing.ButtonGroup btnGruopGender;
    private com.toedter.calendar.JDateChooser dtcBirthday;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblBirthDay;
    private javax.swing.JLabel lblConfirmPass;
    private javax.swing.JLabel lblFullName;
    private javax.swing.JLabel lblGender;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JPasswordField pwfConfirmPass;
    private javax.swing.JPasswordField pwfPass;
    private javax.swing.JRadioButton rdbFemale;
    private javax.swing.JRadioButton rdbMale;
    private javax.swing.JTextField txfFullName;
    private javax.swing.JTextField txfPhone;
    private javax.swing.JTextField txfUserName;
    // End of variables declaration//GEN-END:variables
}
